import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:life_service/color/hex.dart';
import 'package:life_service/utils/styles.dart';

import 'controller.dart';

class SelectDatePage extends GetView<SelectDateController> {
  const SelectDatePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        leadingWidth: 110,
        leading: IconButton(
          onPressed: Get.back,
          icon: Text.rich(
            TextSpan(
              children: [
                WidgetSpan(
                    child: Icon(
                      CupertinoIcons.chevron_back,
                      size: 20,
                      color: HexColor(HexColor.color3333),
                    ),
                    alignment: PlaceholderAlignment.middle),
                TextSpan(
                  text: ' 选择日期',
                  style: StylesUtils.customTextStyle(
                    color: HexColor.color3333,
                    fontSize: StylesUtils.fontSize16,
                  ),
                ),
              ],
            ),
          ),
        ),
        elevation: 1.0,
        shadowColor: HexColor(HexColor.colorF2f2),
        // shape: UnderlineInputBorder(
        //   borderRadius: BorderRadius.zero,
        //   borderSide: BorderSide(
        //     color: HexColor(HexColor.colorF2f2),
        //   ),
        // ),
        actions: [
          IconButton(
            onPressed: () {},
            icon: Text(
              '重置',
              style: StylesUtils.customTextStyle(
                color: HexColor.color3333,
              ),
            ),
          ),
        ],
      ),
      body: CustomScrollView(
        slivers: [
          PinnedHeaderSliver(
            child: CustomPaint(
              size: const Size.fromHeight(80),
              painter: _MyCustomPainter(),
              child: Container(
                height: 80,
                color: Colors.white,
                child: Row(
                  children: [
                    Expanded(
                      child: Column(
                        children: [
                          Container(
                            padding: const EdgeInsets.only(top: 20),
                            alignment: Alignment.center,
                            child: Text(
                              '入住日期',
                              style: StylesUtils.customTextStyle(
                                color: HexColor.colorCccc,
                                fontSize: StylesUtils.fontSize12,
                              ),
                            ),
                          ),
                          Container(
                            padding: const EdgeInsets.only(top: 10),
                            alignment: Alignment.center,
                            child: Text(
                              '2019-12-18',
                              style: StylesUtils.customTextStyle(
                                fontSize: StylesUtils.fontSize16,
                                color: HexColor.color3333,
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                    Expanded(
                      child: Column(
                        children: [
                          Container(
                            padding: const EdgeInsets.only(top: 20),
                            alignment: Alignment.center,
                            child: Text(
                              '退房日期',
                              style: StylesUtils.customTextStyle(
                                color: HexColor.colorCccc,
                                fontSize: StylesUtils.fontSize12,
                              ),
                            ),
                          ),
                          Container(
                            padding: const EdgeInsets.only(top: 10),
                            alignment: Alignment.center,
                            child: Text(
                              '2019-12-19',
                              style: StylesUtils.customTextStyle(
                                fontSize: StylesUtils.fontSize16,
                                color: HexColor.color3333,
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildListDelegate(
              [
                Column(
                  children: [
                    Container(
                      padding: EdgeInsets.only(
                          top: 20, left: (Get.width - 337) / 2, bottom: 15),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '2024年09月',
                        style: StylesUtils.customTextStyle(
                          color: HexColor.color3333,
                          fontWeight: FontWeight.w700,
                          fontSize: StylesUtils.fontSize18,
                        ),
                      ),
                    ),
                    Center(
                      child: SizedBox(
                        width: 337,
                        child: Column(
                          children: [
                            SizedBox(
                              height: 30,
                              child: Row(
                                children: controller.weeks
                                    .map(
                                      (str) => Expanded(
                                        child: Center(
                                          child: Text(
                                            str,
                                            style: StylesUtils.customTextStyle(
                                              color: HexColor.color3333,
                                              fontSize: StylesUtils.fontSize12,
                                              fontWeight: FontWeight.w700,
                                            ),
                                          ),
                                        ),
                                      ),
                                    )
                                    .toList(),
                              ),
                            ),
                            StaggeredGrid.count(
                              crossAxisCount: 7,
                              crossAxisSpacing: 0,
                              mainAxisSpacing: 10,
                              children: [
                                for (var i = 26; i < 32; i++)
                                  Container(
                                    constraints: const BoxConstraints.expand(
                                        width: 30, height: 32),
                                    alignment: Alignment.center,
                                    child: Text(
                                      '',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: StylesUtils.fontSize12,
                                          color: HexColor.color3333),
                                    ),
                                  ),
                                Container(
                                  constraints: const BoxConstraints.expand(
                                      width: 30, height: 32),
                                  alignment: Alignment.center,
                                  child: Text(
                                    '1',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: StylesUtils.fontSize12,
                                        color: HexColor.color3333),
                                  ),
                                ),
                                for (var i = 2; i < 31; i++)
                                  Container(
                                    constraints: const BoxConstraints.expand(
                                        width: 30, height: 32),
                                    alignment: Alignment.center,
                                    decoration: activeBoxDecoration(21, 28, i),
                                    child: Text(
                                      '$i',
                                      style: StylesUtils.customTextStyle(
                                        fontSize: StylesUtils.fontSize12,
                                        color: activeColor(21, 28, i),
                                      ),
                                    ),
                                  ),
                                for (var i = 1; i < 7; i++)
                                  Container(
                                    constraints: const BoxConstraints.expand(
                                        width: 30, height: 32),
                                    alignment: Alignment.center,
                                    child: Text(
                                      '',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: StylesUtils.fontSize12,
                                          color: HexColor.color3333),
                                    ),
                                  ),
                              ],
                            )
                          ],
                        ),
                      ),
                    )
                  ],
                ),
                Column(
                  children: [
                    Container(
                      padding: EdgeInsets.only(
                          top: 20, left: (Get.width - 337) / 2, bottom: 15),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '2024年10月',
                        style: StylesUtils.customTextStyle(
                          color: HexColor.color3333,
                          fontWeight: FontWeight.w700,
                          fontSize: StylesUtils.fontSize18,
                        ),
                      ),
                    ),
                    Center(
                      child: SizedBox(
                        width: 337,
                        child: Column(
                          children: [
                            SizedBox(
                              height: 30,
                              child: Row(
                                children: controller.weeks
                                    .map(
                                      (str) => Expanded(
                                        child: Center(
                                          child: Text(
                                            str,
                                            style: StylesUtils.customTextStyle(
                                              color: HexColor.color3333,
                                              fontSize: StylesUtils.fontSize12,
                                              fontWeight: FontWeight.w700,
                                            ),
                                          ),
                                        ),
                                      ),
                                    )
                                    .toList(),
                              ),
                            ),
                            StaggeredGrid.count(
                              crossAxisCount: 7,
                              crossAxisSpacing: 0,
                              mainAxisSpacing: 10,
                              children: [
                                Container(
                                  constraints: const BoxConstraints.expand(
                                      width: 30, height: 32),
                                  alignment: Alignment.center,
                                  child: Text(
                                    '',
                                    style: StylesUtils.customTextStyle(
                                        fontSize: StylesUtils.fontSize12,
                                        color: HexColor.color3333),
                                  ),
                                ),
                                for (var i = 1; i < 32; i++)
                                  Container(
                                    constraints: const BoxConstraints.expand(
                                        width: 30, height: 32),
                                    alignment: Alignment.center,
                                    child: Text(
                                      '$i',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: StylesUtils.fontSize12,
                                          color: HexColor.color3333),
                                    ),
                                  ),
                                for (var i = 1; i < 4; i++)
                                  Container(
                                    constraints: const BoxConstraints.expand(
                                        width: 30, height: 32),
                                    alignment: Alignment.center,
                                    child: Text(
                                      '',
                                      style: StylesUtils.customTextStyle(
                                          fontSize: StylesUtils.fontSize12,
                                          color: HexColor.color3333),
                                    ),
                                  ),
                              ],
                            )
                          ],
                        ),
                      ),
                    )
                  ],
                )
              ],
            ),
          ),
        ],
      ),
      bottomNavigationBar: SafeArea(
        bottom: true,
        child: Container(
          height: 60,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
              top: BorderSide(
                width: 2,
                color: HexColor(HexColor.colorE6e6),
              ),
            ),
          ),
          child: TextButton(
            onPressed: () {},
            style: ButtonStyle(
              fixedSize: const WidgetStatePropertyAll(
                Size(334, 44),
              ),
              minimumSize: const WidgetStatePropertyAll(
                Size(334, 44),
              ),
              shape: WidgetStatePropertyAll(
                RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(4.r),
                ),
              ),
              backgroundColor: WidgetStatePropertyAll(
                HexColor(HexColor.colorFf62),
              ),
              alignment: Alignment.center,
            ),
            child: Text(
              '确 定',
              style: StylesUtils.customTextStyle(
                fontSize: StylesUtils.fontSize16,
              ),
            ),
          ),
        ),
      ),
    );
  }

  String activeColor(int start, int end, int date) {
    if (start == date || end == date) {
      return HexColor.colorFfff;
    }
    return HexColor.color3333;
  }

  BoxDecoration? activeBoxDecoration(int start, int end, int date) {
    if (start == date || end == date) {
      return BoxDecoration(
        color: HexColor(HexColor.colorFf62),
        borderRadius: BorderRadius.circular(4.r),
      );
    }
    if (start < date && end > date) {
      return BoxDecoration(
        color: HexColor.rgba(255, 98, 98, 0.0980392156862745),
      );
    }
    return null;
  }
}

class _MyCustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..style = PaintingStyle.stroke
      ..color = HexColor(HexColor.colorE6e6)
      ..strokeWidth = 2;
    canvas.drawLine(Offset(0, size.height - 0),
        Offset(size.width - 0, size.height - 0), paint);

    canvas.drawLine(Offset(size.width / 2, (size.height - 30) / 2),
        Offset(size.width / 2, (size.height - 30) / 2 + 30), paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return oldDelegate != this;
  }
}
